// pages/noteInput/noteInput.js
var masterId;
var guahaoMoneyNum;
var funRing = function (e,sendData) {
  e(sendData);
};
var sendData=function(data) {
  console.log(data.out_trade_no,'订单号')
  wx.request({
    url: 'https://www.liuxuanlong.xyz/fenda/wx/addGuahaoHistory',
    data: {
      name: data.name,
      idCard: data.IDcard,
      phoneNumber: data.phoneNumber,
      guahaoDate: data.guahaoDate,
      moneyNum:data.guahaoMoneyNum,
      masterId:masterId,
      out_trade_no:data.out_trade_no,
      openId:getApp().globalData.openid
    },
    success:function(res){
      console.log(res.data)
      wx.showToast({
        title: '预约成功！',
        icon:'success',
        duration:800
       
      })
      wx.redirectTo({
        url: '../noteHistory/noteHistory?advice=' + JSON.stringify(res.data),
      })
    },
    
  })
}
var Time;
var pay=function (fun,sendData) {
  var that = this;
  wx.login({
    success: function (res) {
      if (res.code) {
        //发起网络请求
        wx.request({
          url: 'https://www.liuxuanlong.xyz/fenda/wx/guahaoPay',
          data: {
            code: res.code,
            total_fee: guahaoMoneyNum
          },
          success: function (res) {
          wx.showToast({
            title: '调起支付',
            icon:'loading',
            duration:800
            
          })
            console.log(res.data.out_trade_no, '订单号');
            var out_trade_no = res.data.out_trade_no;
            sendData['out_trade_no'] = out_trade_no;
            wx.requestPayment(
              {
                'appid': res.data.appid,
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                
                  
                  funRing(fun,sendData);
                  console.log("支付成功，执行下个操作",res);
                },
                'fail': function (res) {
                  console.log('打印', res);
                },
                'complete': function (res) {
                  console.log("666")
                }
              })
            /*wx.setStorage({
              key: "login_uuId",
              data: res.data
            });*/


            console.log('登录完毕')
          }
        })
      } else {
        console.log('获取用户登录态失败！' + res.avatarUrl)
      }
    }
  })
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
      loading:"display",
      mainFont:"none"

  },
  getData:function(e){
    console.log(e);
    var IDcard=e.detail.value.IDcard;
    var phoneNumber=e.detail.value.phoneNumber;
    var chooseDate=getApp().globalData.chooseDate;
    var name=e.detail.value.name;
    var sendDatas={};
    sendDatas['IDcard']=IDcard;
    sendDatas["phoneNumber"]=phoneNumber;
    sendDatas["name"]=name;
    sendDatas['masterId']=masterId;
    sendDatas["guahaoMoneyNum"]=guahaoMoneyNum;
    if(Time=="上午") sendDatas["guahaoDate"]=chooseDate+" "+"11:00:00";
    else if(Time=="下午")sendDatas["guahaoDate"]=chooseDate+" "+"16:00:00";
    console.log(masterId,IDcard,phoneNumber,chooseDate,Time,name,'请求的数据');
    if(!IDcard||!phoneNumber)
      wx.showLoading({
        title: '请填写完整',
        icon:"loading",
        duration:800
      })
      else{
      pay(sendData, sendDatas);
      }

  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    masterId=options.masterId;
    Time=options.Time;
    guahaoMoneyNum=options.guahaoMoneyNum;
    this.setData(
      {
        loading:"none",
        mainFont:"display",
        payNum: Number(guahaoMoneyNum)*0.01
      }
    );
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})